package com.ssg.poultry.util;

import com.sun.media.jfxmedia.logging.Logger;

public class ByteUtil {
	private static byte charToByte(char c) {   
	    return (byte) "0123456789ABCDEF".indexOf(c);   
	}
	public static String byteToHex(int i) {
		String str = Integer.toHexString(i);
		while(str.length()<2) {
			str = "0"+str;
		}
		return str;
	}
	public static byte[] hexToBytes(String hex) {
		
		hex = hex.toUpperCase();
		int len = hex.length()/2;
		byte[] data = new byte[len];
		char[] hexChars = hex.toCharArray();
		for(int i=0;i<hex.length();i+=2) {
			byte b = (byte) ((charToByte(hexChars[i]) << 4) | charToByte(hexChars[i+1])) ;
			data[i/2] = b;
		}
		return data;
	}
	public static String bytesToHex(byte[] data) {
		StringBuilder sb = new StringBuilder();
		for(byte b:data) {
			int x = 0xff & b;
			String str = Integer.toHexString(x);
			if(str.length() == 1) {
				sb.append("0");
			}
			sb.append(str);
			sb.append(" ");
		}
		if(sb.length()>0) {
			sb.deleteCharAt(sb.length()-1);
		}
		return sb.toString().toUpperCase();
	}
}
